package com.invoicerecognition.backend.utils;

import com.alibaba.fastjson.JSONObject;
import okhttp3.*;

import java.io.IOException;

public class InvoiceUtil {
    static final OkHttpClient HTTP_CLIENT = new OkHttpClient().newBuilder().build();

    public JSONObject getInvoice(String codeBase64, String paramType) {

        JSONObject result = new JSONObject();
        MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
        String sealTag = "false";
        String requestBodyContent = "";
        if ("url".equals(paramType)) requestBodyContent = "url=" + codeBase64 + "&seal_tag=" + sealTag;
        else requestBodyContent = "image=" + codeBase64 + "&seal_tag=" + sealTag;
        RequestBody body = RequestBody.create(mediaType, requestBodyContent);
        Request request = new Request.Builder()
                .url("https://aip.baidubce.com/rest/2.0/ocr/v1/vat_invoice?access_token=24.04672ca2234e7cce184cc36df83166f5.2592000.1733977990.282335-116206052")
                .method("POST", body)
                .addHeader("Content-Type", "application/x-www-form-urlencoded")
                .addHeader("Accept", "application/json")
                .build();
        String responseBody = ""; // 假设这是你的响应内容，类似于 JSON 字符串
        try {
            Response response = HTTP_CLIENT.newCall(request).execute();
            responseBody = response.body().string();    // 获取 JSON 响应体
            result = JSONObject.parseObject(responseBody);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return result;
    }

}
